package com.outstudio.plan.garbage;

import java.util.ArrayList;
import java.util.List;

import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.outstudio.plan.op.pdf.model.Furniture;
import com.outstudio.plan.op.pdf.model.HomePage;
import com.outstudio.plan.op.pdf.model.Room;
import com.outstudio.plan.op.svg.SvgUtil;

public class SvgOp2 {

	private String tempPath;

	public SvgOp2(String tempPath) {
		if (!tempPath.endsWith("\\") && !tempPath.endsWith("/")) {
			tempPath += "/";
		}
		this.tempPath = tempPath;
	}

	public HomePage getHomePage(Document doc) {
		HomePage hp = new HomePage();
		Element root = doc.getDocumentElement();

		if (root.hasAttribute("biaoti")) {
			hp.setBiaoti(root.getAttribute("biaoti"));
		}
		if (root.hasAttribute("descrition")) {
			hp.setDescription(root.getAttribute("descrition"));
		}

		String hpPath = tempPath + "homePage.png";

		boolean flag = SvgUtil.outputSvgDocToPng(doc, hpPath);

		if (flag) {
			hp.setImage(hpPath);
		}

		return hp;
	}

	// 未完成
	public List<Room> getRoom(Document doc) {
		Element root = doc.getDocumentElement();
		List<Room> rooms = new ArrayList<>();

		NodeList childList = root.getChildNodes();

		if (childList.getLength() == 0) {
			return null;
		}

		for (int i = 0; i < childList.getLength(); i++) {
			Node roomNode = childList.item(i);
			if (!roomNode.hasAttributes()) {
				continue;
			}
			NamedNodeMap nnm = roomNode.getAttributes();

			// 用uniqueId确定是否是room
			Node uniqueId = nnm.getNamedItem("uniqueId");
			if (uniqueId == null) {
				continue;
			}
			Room room = new Room();
			Node id = nnm.getNamedItem("id");
			room.setId(id.getNodeValue());
			Node name = nnm.getNamedItem("name");
			room.setName(name.getNodeValue());
			Node description = nnm.getNamedItem("description");
			room.setDescription(description.getNodeValue());
			// room.setData(data);
			List<Furniture> furns = getFurniture(roomNode);
			room.setFurns(furns);
			String roomImagePath = tempPath + id + (System.currentTimeMillis() + "").substring(6) + ".png";
			try {
				createNodePng(roomNode, roomImagePath);
			} catch (Exception e) {
				roomImagePath = null;
				e.printStackTrace();
			}
			room.setImage(roomImagePath);
			// room.setThumbnail(thumbnail);

			rooms.add(room);
		}

		return rooms;
	}

	public List<Furniture> getFurniture(Node roomNode) {
		List<Furniture> furns = new ArrayList<>();
		NodeList childList = roomNode.getChildNodes();

		for (int i = 0; i < childList.getLength(); i++) {
			Node childNode = childList.item(i);
			NamedNodeMap cnnm = childNode.getAttributes();
			Node uniqueItemId = cnnm.getNamedItem("item-unique");
			if (uniqueItemId == null) {
				continue;
			}
			Furniture furn = new Furniture();
			furn.setId(uniqueItemId.getNodeValue());

			Node name = cnnm.getNamedItem("name");
			if (name != null) {
				furn.setName(name.getNodeValue());
			}
			Node description = cnnm.getNamedItem("name");
			if (description != null) {
				furn.setDescription(description.getNodeValue());
			}
			String uniqueFurnPath = tempPath + name.getNodeValue() + (System.currentTimeMillis() + "").substring(6)
					+ ".png";
			try {
				createNodePng(childNode, uniqueFurnPath);
			} catch (Exception e) {
				uniqueFurnPath = null;
				e.printStackTrace();
			}
			furn.setImage(uniqueFurnPath);

			furns.add(furn);
		}

		return furns;
	}

	public void createRoomThumbnail(Node roomNode) {

	}

	public void createNodePng(Node childNode, String fpath) {
		SVGDOMImplementation dom = new SVGDOMImplementation();
		Document doc = dom.createDocument(dom.SVG_NAMESPACE_URI, "svg", null);
		Element root = doc.getDocumentElement();
		root.appendChild(childNode);
		SvgUtil.outputSvgDocToPng(doc, fpath);
	}

}
